package com.jtzc.mcp.coze.domain.service;

import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONUtil;
import com.coze.openapi.client.workflows.run.RunWorkflowResp;
import com.jtzc.mcp.coze.domain.adapter.CozeWorkFlowPort;
import com.jtzc.mcp.coze.domain.mocel.GoodsGenerateImagesRequest;
import com.jtzc.mcp.coze.domain.mocel.GoodsGenerateImagesResponse;
import com.jtzc.mcp.coze.infrastructure.dto.CozeUploadDto;
import com.jtzc.mcp.coze.infrastructure.dto.CozeWorkflowDto;
import com.jtzc.mcp.coze.dao.Goods;
import com.jtzc.mcp.coze.dao.GoodsDao;
import com.jtzc.mcp.coze.infrastructure.servce.CozeClinetService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wu chuang
 * @description
 */
@Slf4j
@Service
public class CozeWorkFlowService {


    @Resource
    private CozeWorkFlowPort cozeWorkFlowPort;

    @Resource
    private CozeClinetService cozeClinetService;

    @Autowired
    private GoodsDao goodsDao;

    @Tool(description = "调用coze工作流,给商品生成图片")
    public GoodsGenerateImagesResponse generateImages(GoodsGenerateImagesRequest request) throws IOException {
        CozeWorkflowDto dto = new CozeWorkflowDto();
        dto.setWorkflow_id("7534231159518117926");
        dto.setApp_id("7534183129280725038");
        // coze 工作流入参
        dto.setInput(request.getInput());
        Map<String, String> map = new HashMap<>();
        map.put("prompt",request.getInput());
        map.put("BOT_USER_INPUT",request.getInput());
        dto.setExt(map);
        // 调用coze api
        RunWorkflowResp runWorkflowResp = cozeWorkFlowPort.runCozeWorkflow(dto);
        GoodsGenerateImagesResponse response = new GoodsGenerateImagesResponse();
        log.info("文生图返回值:{}", JSONUtil.toJsonStr(runWorkflowResp));
        String string = JSONUtil.parseObj(JSONUtil.parseObj(runWorkflowResp.getData()).get("data").toString()).get("output").toString();
        response.setData(string);
        Goods goods = new Goods();
        goods.setName(request.getInput());
        goods.setDescription(response.getData());
        try {
            goodsDao.insert(goods);
        }catch (Exception e) {
            log.error("insert error" , e);
        }

        return response;
    }


    @Tool(description = "调用coze工作流,给商品生成视频")
    public GoodsGenerateImagesResponse generateVido(GoodsGenerateImagesRequest request) throws IOException {
        GoodsGenerateImagesResponse response = new GoodsGenerateImagesResponse();
        CozeUploadDto uploadDto = new CozeUploadDto();
        uploadDto.setFile_name(UUID.fastUUID().toString());
        uploadDto.setFile_url(request.getUrl());
        // 上传图片
        HashMap<String, String> fileIdMap = cozeClinetService.uploadFileToCoze(uploadDto);
        String fileId = fileIdMap.get("file_id");
        log.info("文件上传获取到到file_id: {}", fileId);
        CozeWorkflowDto dto = new CozeWorkflowDto();
        dto.setWorkflow_id("7534257470911954963");
        dto.setApp_id("7534214947631267881");
        dto.setInput(request.getInput());
        dto.setFile_id(fileId);
        dto.setExt(fileIdMap);
        // 生成视频
        RunWorkflowResp runWorkflowResp = cozeWorkFlowPort.runCozeWorkflow(dto);

        log.info("文生视频返回值:{}", JSONUtil.toJsonStr(runWorkflowResp));
        response.setData(JSONUtil.parseObj(runWorkflowResp.getData()).get("url").toString());
        Goods goods = new Goods();
        goods.setName(request.getInput());
        goods.setDescription(response.getData());
        try {
            goodsDao.insert(goods);
        }catch (Exception e) {
            log.error("insert error" , e);
        }

        return response;
    }

}
